//
//  MainViewController_iPad.m
//  PhongThuyHuongNha
//
//  Created by Le Anh Tai on 9/28/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "MainViewController_iPad.h"
#import <CoreLocation/CLHeading.h>
#import <QuartzCore/QuartzCore.h>
#import "math.h"
#import "UIConstants.h"
#import "OwnerInfoCalloutView_iPad.h"
#import "HeaderSectionResultView_iPad.h"
#import "Utility.h"
#import "HuongInfoCell_iPad.h"
#import "FooterResultView_iPad.h"
#import <PhongThuy/PhongThuy.h>

@interface MainViewController_iPad ()

@end

@implementation MainViewController_iPad

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self)
    {
        
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _ownerInfoFooterView = [[FooterResultView_iPad alloc] init];
    _ownerInfoFooterView.delegate = self;
    _ownerInfoFooterView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [_explainationView addSubview:_ownerInfoFooterView];        
}

- (OwnerInfoCalloutView *)newOwnerInfoCalloutView
{
    return [[OwnerInfoCalloutView_iPad alloc] init];
}

- (void)showResult
{
    [super showResult];
    [_ownerInfoCallout close];
}

- (void)infoButtonPressed:(id)sender
{
    [super infoButtonPressed:sender];
}

- (void)OwnerInfoCalloutViewDidPressedOutSide:(OwnerInfoCalloutView *)view
{
    [self OwnerInfoCalloutViewDidPressedXemButton:view];
}

- (void)OwnerInfoCalloutViewDidPressedXemButton:(OwnerInfoCalloutView *)view
{
    //
    [_ownerInfoCallout close];
    
    NSMutableString *ownerInfo = [[NSMutableString alloc] init];
    
    [ownerInfo appendFormat:@"%@, ",[_fengShuiCore genderString]];
    _homeDirection.phiCung.cung = [_fengShuiCore getPhiCung];
    
    if (_ownerInfoCallout.isAmLich)
    {
        [ownerInfo appendFormat:@"sinh ngày %@ AL, năm %@ %@, bản mệnh: %@.",[_fengShuiCore.lunarDate stringddmmyyyy ], [_fengShuiCore getCan], [_fengShuiCore getChi], [_fengShuiCore getMang]];
    }
    else
    {
        [ownerInfo appendFormat:@"sinh ngày %@ nhằm %@ AL, năm %@ %@, bản mệnh: %@.",[_ownerInfoCallout.birthday stringddmmyyyy],[_fengShuiCore.lunarDate stringddmmyyyy ], [_fengShuiCore getCan], [_fengShuiCore getChi],[_fengShuiCore getMang]];
    }
    
    [ownerInfo appendFormat:@" Phi Cung '%@', thuộc '%@' tứ trạch.\n", [_fengShuiCore getPhiCung], [_homeDirection.phiCung getThuocTuTrach]];
    
    [_ownerInfoHeaderView loadOwnerInfo:ownerInfo];
    _ownerInfoHeaderView.frame = _directionTableView.bounds;
    
    if (_huongInfos == nil)
    {
        _huongInfos = [[NSMutableArray alloc] init];
    }
    
    [_huongInfos removeAllObjects];
    
    NSArray *huongTot = [_homeDirection get4HuongTot];
    NSArray *huongXau = [_homeDirection get4HuongXau];
    
    if (huongTot)
    {
        [_huongInfos addObject:huongTot];
    }
    
    if (huongXau)
    {
        [_huongInfos addObject:huongXau];
    }
    
    //
    NSString *batQuaiImageName = @"";
    NSInteger quaiSoIdx = _homeDirection.phiCung.index;
    if (quaiSoIdx > -1)
    {
        batQuaiImageName = [NSString stringWithFormat:@"batquai_%d.png", quaiSoIdx+1];
    }
    
    NSLog(@"Bat quai image: %@", batQuaiImageName);
    [self updateBatQuaiImage:batQuaiImageName];

    //
    [_directionTableView reloadData];
    [_directionTableView setTableHeaderView:_ownerInfoHeaderView];
}

#pragma mark - Delegate UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[HeaderSectionResultView_iPad alloc] init];
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *huongs = [Utility getObject:_huongInfos atIndex:section];
    
    if (huongs == nil)
    {
        return 0;
    }
    
    return huongs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    HuongInfoCell_iPad *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil)
    {
        cell = [[HuongInfoCell_iPad alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSArray *huongTot = [Utility getObject:_huongInfos atIndex:SECTION_HUONG_TOT];
    if (huongTot)
    {
        [cell loadHuongTot:[Utility getObject:huongTot atIndex:indexPath.row]];
    }
    
    NSArray *huongXau = [Utility getObject:_huongInfos atIndex:SECTION_HUONG_XAU];
    if (huongXau)
    {
        [cell loadHuongXau:[Utility getObject:huongXau atIndex:indexPath.row]];
    }
    
    
    [cell setTextTotColor:COLOR_BLUE_IOS7 xauColor:COLOR_LIGHT_BLACK];
    
    return cell;
}

#pragma mark - Rotation methods
// This method was deprecated in iOS 6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Overriden to allow any orientation.
    return YES;
}

#pragma mark Orientation in iOS 6.0
// Apple replaced shouldAutorotateToInterfaceOrientation method with 02 other methods: supportedInterfaceOrientations, shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload
{
    [self setDirectionTableView:nil];
    [super viewDidUnload];
}
@end
